#ifndef MAP_TABLE_H
#define MAP_TABLE_H

#include "list_table.h"

template <typename _key, typename _value>
class MapTable
{
public:
    struct KeyValue
    {
        KeyValue() {}
        KeyValue(_key k)
            : key(k), value{0}
        {}

        _key   key;
        _value value;
    };
    MapTable()
        :  mSize(0)
    {}

    typedef typename ListTable<KeyValue>::iterator iterator;

    _value &operator[] (_key key)
    {
        typename ListTable<KeyValue>::iterator it;
        for (it = mMap.begin(); it != mMap.end(); ++it)
        {
            if (it->key == key)
            {
                return it->value;
            }
        }

        push_back(KeyValue(key));

        return mMap.back().value;
    }

    bool empty()
    {
        return mMap.empty();
    }

    iterator begin()
    {
        return mMap.begin();
    }
    iterator end()
    {
        return mMap.end();
    }

    void clear()
    {
        mSize = 0;
        mMap.clear();
    }

    int size()
    {
        return mSize;
    }
    void remove(iterator it)
    {
        --mSize;
        mMap.remove(it);
    }

    bool remove (const _key &key)
    {
        --mSize;
        typename ListTable<KeyValue>::iterator it;
        for (it = mMap.begin(); it != mMap.end(); ++it)
        {
            if (it->key == key)
            {
                remove(it);
                return true;
            }
        }
        return false;
    }

    void move(iterator it, MapTable &map)
    {
        it._M_node->del();
        map.push_back(it);
    }

protected:
    void push_back(iterator it)
    {
        ++mSize;
        mMap.push_back(it);
    }

    void push_back(KeyValue const &kv)
    {
        ++mSize;
        mMap.push_back(kv);
    }

private:
    int                 mSize;
    ListTable<KeyValue> mMap;    
};


#endif // MAP_TABLE_H
